// 理赔 

const { getClaims, addClaims, modifyClaims, delClaims } = require('../dao/claimsDao')
const { moveFiles, removeFiles } = require('../utils/handleFiles')


module.exports.getClaims = async param => {
    try {
        const {data,total,pageTotal} = await getClaims(param)
        return { message: '成功', data, status: 1 ,total,pageTotal}
    } catch (error) {
        return { message: '服务器错误', error, status: 0 ,total,pageTotal }
    }
}



//添加理赔start
module.exports.addClaims = async param => {
    try {
        const data = await addClaims(param)
        if (data._id) {
            if (param.images instanceof Array) {
                param.images.forEach(element => {
                    moveFiles({
                        fromPath: './public/temp',
                        toPath: './public/images',
                        filename: element
                    });
                });
                removeFiles('./public/temp');
            } else {
                moveFiles({
                    fromPath: './public/temp',
                    toPath: './public/images',
                    filename: param.images
                });
                removeFiles('./public/temp');
            }
            return {
                message: '添加成功',
                status: 1
            }
        } else {
            return {
                message: '添加失败',
                status: 0
            }
        }
        // return { message: '添加成功', data, status: 1 }
    } catch (error) {
        return { message: '服务器错误', error, status: 0 }
    }
}
//end

//修改理赔信息

/**
 * 
 * @param 接收{_id,update:{}}
 */
module.exports.modifyClaims = async param => {
    try {
        const { _id } = await modifyClaims(param)
        if (_id) {
              if(param.update.images){
                moveFiles({
                    fromPath: './public/temp',
                    toPath: './public/images',
                    filename: param.update.images
                });
                removeFiles('./public/temp');
              }
            return { message: '修改成功', status: 1 }
        } else {
            return { message: '修改失败', status: 0 }
        }
    } catch (error) {
        return { message: '服务器错误', status: 0 }
    }
}

//删除理赔
module.exports.delClaims = async id => {
    try {
        const { _id } = await delClaims(id);
        if (_id) {
            return { message: '删除成功', status: 1 }
        } else {
            return { message: '申请中的理赔不能删除', status: 0 }
        }
    } catch (error) {
        return { message: '服务器错误', status: 0 }
    }
}